package com.lanou.dllo.shise.com.lanou.dllo.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;

import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.application.SysApplication;
import com.lanou.dllo.shise.com.lanou.dllo.fragment.FavFragment;
import com.lanou.dllo.shise.com.lanou.dllo.fragment.HomeFragment;

import com.lanou.dllo.shise.com.lanou.dllo.fragment.MyFragment;
import com.lanou.dllo.shise.com.lanou.dllo.fragment.SearchFragment;
import com.lanou.dllo.shise.com.lanou.dllo.receiver.OtherBroadcastReceiver;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private OtherBroadcastReceiver receiver;
    private TabHost tabHost;//定义一个tabHost组件
    private FragmentManager fragmentManager;//定义一个碎片化管理对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SysApplication.getInstance().addActivity(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        initView();
        initData();
        //定义要进行动态注册的接收器对象
        receiver = new OtherBroadcastReceiver();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_gallery) {
            showAlertDialog();
            // Handle the camera action
        } else if (id == R.id.nav_manage) {
            Intent intent = new Intent(this, ConfigurationActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_view) {
            Intent intent = new Intent(this,MessageActivity.class);
            startActivity(intent);

        } else if (id == R.id.nav_share) {
            Intent intent = new Intent(this,Feed_backActivity.class);
            startActivity(intent);

        } else if (id == R.id.nav_send) {
            //关闭整个程序
            SysApplication.getInstance().exit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;


    }

    public void showAlertDialog() {
        //创建一个AlertDialog.Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("切换用户");//标题文字
        builder.setIcon(R.mipmap.icon);//标题边上的图标
        builder.setMessage("确定切换用户?");//设置内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.create().show();//弹出显示提示框

    }





    private void initView() {


        tabHost = (TabHost) findViewById(android.R.id.tabhost);//找到系统的TabHost id
        tabHost.setup();//初始化tabHost
        fragmentManager = getSupportFragmentManager();
        //添加tab标签
        TabHost.TabSpec tabhome = tabHost.newTabSpec("home");
        View homeview = getLayoutInflater().inflate(R.layout.tab_home_indictor, null
        );
        tabhome.setIndicator(homeview);
        tabhome.setContent(R.id.homeView);
        tabHost.addTab(tabhome);

        TabHost.TabSpec tabsearch = tabHost.newTabSpec("search");
        View searchview = getLayoutInflater().inflate(R.layout.tab_search_indictor, null);
        tabsearch.setIndicator(searchview);
        tabsearch.setContent(R.id.searchView);
        tabHost.addTab(tabsearch);


        TabHost.TabSpec tabfav = tabHost.newTabSpec("fav");
        View favview = getLayoutInflater().inflate(R.layout.tab_fav_indictor, null);
        tabfav.setContent(R.id.favView);
        tabfav.setIndicator(favview);
        tabHost.addTab(tabfav);

        TabHost.TabSpec tabmy = tabHost.newTabSpec("my");
        View myview = getLayoutInflater().inflate(R.layout.tab_my_indictor, null);
        tabmy.setIndicator(myview);
        tabmy.setContent(R.id.myView);
        tabHost.addTab(tabmy);

    }

    private void initData() {

        //开始业务对象
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.homeView, new HomeFragment());
        fragmentTransaction.replace(R.id.searchView, new SearchFragment());
        fragmentTransaction.replace(R.id.favView, new FavFragment());
        fragmentTransaction.replace(R.id.myView, new MyFragment());
        fragmentTransaction.commit();
    }
}




